2.8 EEPROM Data memory

PIC16F84 has 64 bytes of EEPROM memory locations on addresses from 00h to 63h that can be written to or read from. The most important characteristic of this memory is that it does not loose its contents during supply. That practically means that what is written to it remains even if microcontroller is turned off. Data can be retained in EEPROM without supply for up to 40 years (as maker of PIC16F84 microcontroller says), and up to 10000 cycles of writing can be executed. 

In practice, EEPROM memory is used for storing important data or some process parameters. One such parameter is a given temperature, assigned when setting up a temperature regulator to some process. In case that this data isn't retained, it will be necessary to adjust a given temperature after each loss of supply. Since this is very impractical (and even dangerous), makers of microcontrollers have began installing one smaller type of EEPROM memory. 

EEPROM memory is contained in a special memory space and can be accessed through special registers. These registers are:

EEDATA at address 08h, which holds data that is read or that needs to be written.
EEADR at address 09h, which contains an address of EEPROM location being accessed.
EECON1 at address 88h, which contains control bits.
EECON2 at address 89h. This register does not exist physically and serves to protect EEPROM from accidental writing.

EECON1 register at address 88h is a control register with five applied bits.
Bits 5, 6 and 7 are not used, and when read always are zero. Interpretation of EECON1 register bits follows.

EECON1 Register

bit 0 RD (Read Control bit) 
Setting this bit initializes transfer of data from address defined in EEADR to EEDATA register. Since time is not as essential in reading data as in writing, data from EEDATA can already be used further in the next instruction.
1=initializes reading
0=does not initialize reading

bit 1 WR (Write Control bit) 
Setting of this bit initializes writing data from EEDATA register to the address on EEADR register. 
1=initializes writing
0=does not initialize writing

bit 2 WREN (EEPROM Write Enable bit) Enables writing to EEPROM
If this bit is not set, microcontroller will not allow writing to EEPROM.
1=writing allowed
0=writing disallowed

bit 3 WRERR (EEPROM Error Flag bit) Error during writing to EEPROM
This bit is set only in cases when writing to EEPROM was interrupted by a reset signal or by running out of time in watchdog timer (if it's activated).
1=error occured
0=error did not occur

bit 4 EEIF (EEPROM Write Operation Interrupt Flag bit) Bit used to inform that writing data to EEPROM has ended.
When writing has terminated, this bit will be set automatically. Programmer must reset EEIF bit in his program in order to detect new termination of writing. 
1=writing terminated
0=writing not terminated yet, or has not started

Reading from EEPROM Memory

Setting the RD bit initializes transfer of data from address found in EEADR register to EEDATA register. As in reading data we don't need so much time as in writing, data taken over from EEDATA register can already be used further in the next instruction. 

Sample of the part of a program which reads data in EEPROM, could look something like the following:

After the last program instruction, contents from an EEPROM address zero can be found in working register w. 

Writing to EEPROM Memory

In order to write data to EEPROM location, programmer must first write address to EEADR register and data to EEDATA register. Only then is it useful to set WR bit which sets the whole action in motion. WR bit will be reset, and EEIF bit set following a writing which may be used in processing interrupts. Values 55h and AAh are the first and the second key which make it impossible for accidental writing to EEPROM to occur. These two values are written to EECON2 which serves only that purpose, to receive these two values and thus prevent any accidental writing to EEPROM memory. Program lines marked as 1, 2, 3, and 4 must be executed in that order in even time intervals. Therefore, it is very important to turn off interrupts which could change the timing needed for executing instructions. After writing, interrupts can be enabled again in the end.

Example of the part of a program which writes data 0xEE to first location in EEPROM memory could look something like the following:

It is recommended that WREN be turned off the whole time except when writing data to EEPROM, so that possibility of accidental writing would be minimal. 
All writing to EEPROM will automatically clear a location prior to writing anew!